﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace miniQQ_CSharp_SDK
{
    internal class InviteSystem
    {
        JObject jo = new JObject();
        public Root root = new Root();
        string path = @".\Config\邀请系统\邀请系统.json";
        public InviteSystem()
        {
            Init();
        }
        public void Init()
        {
            if (JsonReady())
            {
                if(!JsonLinqExt.TryGetValue(jo, "root", out root))
                {
                    root = new Root();
                }
            }
        }
        public bool JsonReady()
        {
            if (File.Exists(path))
            {
                using (StreamReader sr = new StreamReader(path))
                {
                    string json = sr.ReadToEnd();
                    try
                    {
                        this.jo = JObject.Parse(json);
                        return true;
                    }
                    catch
                    {
                        return false;
                    }

                }
            }
            else return false;
            
        }
        public void WriteJson()
        {
            using(StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine(ParseToJObject().ToString());
            }
        }

        public JObject ParseToJObject()
        {
            JObject jo = new JObject();
            JsonLinqExt.Add(jo, "root", this.root);
            return jo;
        }
        public void AddNewUser(string UserQQ,string QQGroup)
        {
            User user = new User(UserQQ);
            if(GetQQGroup(QQGroup) != null)
            {
                GetQQGroup(QQGroup).Users.Add(user);
            }
            else
            {
                AddNewQQGroup(QQGroup);
                GetQQGroup(QQGroup).Users.Add(user);
            }
        }

        public void AddNewQQGroup(string GroupNum)
        {
            QQGroup qQGroup = new QQGroup(GroupNum);
            if(GetQQGroup(GroupNum) == null)
            {
                root.QQGroups.Add(qQGroup);
            }
            
        }
        public User GetUser(string UserQQ,string QQGroup)
        {
            if(GetQQGroup(QQGroup) != null)
            {
                foreach (User user in GetQQGroup(QQGroup).Users)
                {
                    if (user.UserQQ == UserQQ)
                    {
                        return user;
                    }
                }
            }
            User _user = new User(UserQQ);
            GetQQGroup(QQGroup).Users.Add(_user);
            return _user;
        }
        public QQGroup GetQQGroup(string GroupNum)
        {
            foreach(var qQGroup in root.QQGroups)
            {
                if(qQGroup.GroupNum == GroupNum)
                {
                    return qQGroup;
                }
            }
            QQGroup _qQGroup = new QQGroup(GroupNum);
            root.QQGroups.Add(_qQGroup);
            return _qQGroup;
        }

        public void Leave(string UserQQ, string GroupNum)
        {
            if (GetQQGroup(GroupNum) != null && GetUser(UserQQ, GroupNum) != null)
            {
                GetQQGroup(GroupNum).Leave(GetUser(UserQQ, GroupNum));
            }
        }

        public string InviteSearch(string UserQQ, string GroupNum)
        {
            User user = GetUser(UserQQ,GroupNum);
            int InviteCountAll = user.InvitedUsersAll.Count();
            int InviteCount = user.InvitedUsers.Count();
            int LeaveCount = InviteCountAll - InviteCount;

            string result = AddOn.AtQQ(UserQQ) + "\r\n";
            result += "邀请人数：" + InviteCountAll + "\r\n";
            result += "有效人数：" + InviteCount + "\r\n";
            result += "退群人数：" + LeaveCount + "\r\n";
            result += "邀请人：" + user.InvitedBy;
            return result;
        }

        public string InviteDetail(string UserQQ, string GroupNum)
        {
            User user = GetUser(UserQQ, GroupNum);
            string result = "";
            foreach (string InvitedUserQQ in user.InvitedUsers)
            {
                User InvitedUser = GetUser(InvitedUserQQ, GroupNum);
                result += InvitedUser.GetInTime + "邀请了" + InvitedUser.UserQQ + "\r\n";
            }
            if (result == "") result = "暂无记录";
            return result;
        }

        public string InviteRank(string RobotQQ,string GroupNum)
        {
            string result = "";
            QQGroup qQGroup = GetQQGroup(GroupNum);

            if(qQGroup != null)
            {
                Dictionary<string,int> dic = new Dictionary<string,int>();
                for (int i = 0; i < qQGroup.Users.Count; i++)
                {
                    dic.Add(qQGroup.Users[i].UserQQ, qQGroup.Users[i].InvitedUsers.Count);
                }
                
                int count = 1;
                var sortdic = dic.OrderByDescending(o => o.Value).ToDictionary(o => o.Key, p => p.Value);//按Value值降序
                foreach (var s in sortdic)
                {
                    result += count + ". " + Api.ApiGetNick(RobotQQ, s.Key) + "(" + s.Key + ")：" + s.Value + "\r\n";
                    count++;
                    if (count > 10) break;
                }
            }
            if (result == "") result = "暂无记录";
            return result;
        }
        public string InviteMenu()
        {
            return "╔[Face12.gif]邀请统计[Face129.gif]\n├一一一一一\n┋[emoji=F09F91BD]邀请查询\n┋[emoji=F09F91BD]邀请查询 QQ\n┋[emoji=F09F91BD]邀请记录\n┋[emoji=F09F91BD]邀请记录 QQ\n┋[emoji=F09F91BD]清空邀请 QQ\n┋[emoji=F09F91BD]清空邀请\r\n┋[emoji=F09F91BD]邀请排行\n┕一一一一一";
        }

        public string RemoveAll(string GroupNum)
        {
            root.QQGroups.Remove(GetQQGroup(GroupNum));
            WriteJson();
            return "清除成功！";
        }
        public string RemoveOne(string UserQQ,string GroupNum)
        {
            GetQQGroup(GroupNum).Users.Remove(GetUser(UserQQ,GroupNum));
            WriteJson();
            return "清除成功！";
        }
    }
}
